package com.itouch.bracelet.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.activity.ImageBrowseActivity;
import com.itouch.bracelet.bean.CircleInfo;
import com.itouch.bracelet.bean.CircleReply;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.util.CommonMethodTools;
import com.itouch.widget.TitlePopup;
import com.itouch.widget.TitlePopup.OnItemOnClickListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyAdapter extends BaseAdapter implements OnItemClickListener,OnClickListener{
	List<CircleInfo> data;
	Context mContext;
	private Handler mHandler;
	private DisplayImageOptions option;
	private OnClickListener listener;

	public MyAdapter(List<CircleInfo> list, Context context,Handler h) {
		data = list;
		this.mContext = context;
		mHandler=h;
		option=new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisc(true)
				.build();
		listener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg=mHandler.obtainMessage(9);
				msg.obj=v.getTag();
				msg.setTarget(mHandler);
			}
		};
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
			if(convertView==null){
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.circle_item, null);
			}
			
			GridView gridView = (GridView) convertView.findViewById(R.id.gridView);
			gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
			//gridView.setAdapter(new ImageAdapter(context));
			ImageView logo=(ImageView) convertView.findViewById(R.id.logo);
			ImageView img=(ImageView) convertView.findViewById(R.id.image);
			TextView name = (TextView) convertView.findViewById(R.id.name);
			TextView time = (TextView) convertView.findViewById(R.id.publish_time);
			LinearLayout replyll=(LinearLayout) convertView.findViewById(R.id.replyll);
			
			ImageLoader loader=ImageLoader.getInstance();
			if(data.get(position).getLogopath()==null||data.get(position).getLogopath().equals("")){
				loader.displayImage("drawable://" + R.drawable.m_default_avatar, logo,option);//发帖人头像
			}else{
				loader.displayImage(data.get(position).getLogopath(), logo,option);//发帖人头像	
			}
			
			
			String im=data.get(position).getPic();
			String[] images=im.split(",");
			if(im.equals("")||images.length==0){
				gridView.setVisibility(View.GONE);
			}else{
				
				gridView.setVisibility(View.VISIBLE);
				
				int size=images.length;
				if(size==1){
					gridView.setPadding(0, 0, 20, 0);
					gridView.setNumColumns(1);
					gridView.setAdapter(new MyImageAdapter(images, mContext,2));
				}else{
					gridView.setPadding(0, 0, 20, 0);
					gridView.setNumColumns(3);
					gridView.setAdapter(new MyImageAdapter(images, mContext,3));
				}
				
				
				gridView.setTag(images);
				gridView.setOnItemClickListener(this);
			}
			
			logo.setTag(data.get(position).getCreator());
			logo.setOnClickListener(listener);
			

			name.setText(data.get(position).getName());
			time.setText(data.get(position).getCreateTime());
			if((data.get(position).getReplyInfo()!=null&&data.get(position).getReplyInfo().size()>0)||!data.get(position).getPraiseUser().equals("")){
				replyll.setVisibility(View.VISIBLE);
				replyll.removeAllViewsInLayout();
				if(!data.get(position).getPraiseUser().equals("")){
					LinearLayout ll=(LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.reply_adapter, null);
					TextView tv1=(TextView) ll.findViewById(R.id.replyname);
					tv1.setGravity(Gravity.CENTER_VERTICAL);
					Drawable drawable= mContext.getResources().getDrawable(R.drawable.praise_icon);  
					/// 这一步必须要做,否则不会显示.  
					drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
					tv1.setCompoundDrawables(drawable,null,null,null);  
					tv1.setText(" "+data.get(position).getPraiseUser());
					replyll.addView(ll);
				}
				
				int i=0;
				
				if(data.get(position).getReplyInfo()!=null){
					
					for(CircleReply temp:data.get(position).getReplyInfo()){
						LinearLayout ll=(LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.reply_adapter, null);
						TextView tv1=(TextView) ll.findViewById(R.id.replyname);
						TextView tv2=(TextView) ll.findViewById(R.id.replycontent);
						if(temp.getFlag().equals("")){
							tv1.setText(temp.getUsername()+" 回复 "+temp.getFromUser()+": ");
						}else{
							tv1.setText(temp.getUsername()+": ");
						}
						tv2.setText(temp.getContent());
						ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
						
						temp.setCommentPos(i);
						i++;
						
						temp.setBlogPos(position);
						
						ll.setTag(temp);
						ll.setOnClickListener(this);
						
						replyll.addView(ll);
					}
					
				}
				
				
			}else{
				replyll.setVisibility(View.GONE);
			}
			final TextView tv = (TextView) convertView
					.findViewById(R.id.content);
			final TextView more = (TextView) convertView
					.findViewById(R.id.more);
			final ImageButton button = (ImageButton) convertView
					.findViewById(R.id.imgButton);
			tv.setText(data.get(position).getContent());
			tv.setTag(data.get(position).getTitle());
			/*if(StringUtils.isNotBlank(data.get(position).getTitle())&&data.get(position).getTitle().contains("http")){
				tv.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.circle_back));
			}
			tv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					String url=(String) arg0.getTag();
					if(StringUtils.isNotBlank(url)&&url.contains("http")){
						Message m=new Message();
						m.obj=url;
						m.what=0x998855;
						mHandler.sendMessage(m);
					}
				}
			});
			*/
			/*button.setTag(data.get(position).getId()+"&"+position);*/
			
			button.setTag(data.get(position).getId()+"&"+position);
			
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TitlePopup popup=new TitlePopup(mContext, CommonMethodTools.dip2px(mContext, 180), CommonMethodTools.dip2px(mContext, 40),(String) v.getTag());
					popup.setAnimationStyle(R.style.cricleBottomAnimation);
					popup.setItemOnClickListener(new OnItemOnClickListener() {
						
						@Override
						public void praise(String id) {
							// TODO Auto-generated method stub
							Message msg=mHandler.obtainMessage(2);
							msg.obj=id;
							msg.sendToTarget();
						}
						
						@Override
						public void comment(String id) {
							// TODO Auto-generated method stub
							Message msg=mHandler.obtainMessage(3);
							msg.obj=id;
							msg.sendToTarget();
						}
					});
					popup.show(v,!data.get(position).isPraiseFlag());
				}
			});
			
			more.setTag(false);
			more.getViewTreeObserver().addOnPreDrawListener(
					new OnPreDrawListener() {

						@Override
						public boolean onPreDraw() {
							// TODO Auto-generated method stub
							boolean flag=(Boolean) more.getTag();
							
							if (tv.getLineCount() >= 4&&!flag) {
								more.setVisibility(View.VISIBLE);
							}else{
								more.setVisibility(View.GONE);
							}
							return true;
						}
					});
			more.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					tv.setLines(tv.getLineCount());
					tv.setMaxLines(10);
					more.setTag(true);
					//arg0.setVisibility(View.INVISIBLE);
					//tv.setEllipsize(android.text.TextUtils.TruncateAt.END);
					
				}
			});
		return convertView;
	}

	
	public void modifyData(List<CircleInfo> list){
		
		data=list;
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		switch(parent.getId()){
		case R.id.gridView:
			Intent in=new Intent(mContext, ImageBrowseActivity.class);
			in.putExtra("images", (String[])parent.getTag());
			in.putExtra("pos", position);
			mContext.startActivity(in);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		CircleReply reply=(CircleReply) v.getTag();
		if(reply.getCreator().equals(UserCache.getOauth(mContext).getUserId())){
			//我的回复，点击删除
			Message msg=mHandler.obtainMessage();
			msg.what=4;
			msg.obj=reply;
			msg.sendToTarget();
		}else{
			//回复此评论
			Message msg=mHandler.obtainMessage();
			msg.what=5;
			msg.obj=reply;
			msg.sendToTarget();
		}
	}
	
	
	
}
